package whf.framework.log.util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.sql.DataSource;

import whf.framework.cache.Cache;
import whf.framework.cache.CacheManagerImp;
import whf.framework.entity.Entity;
import whf.framework.log.Log;
import whf.framework.log.LogFactory;
import whf.framework.log.entity.Logable;
import whf.framework.util.BeanFactory;
import whf.framework.util.TypeUtils;

/**
 * @author king
 * @create 2008-1-25 上午11:37:08
 * 
 */
@SuppressWarnings("unchecked")
public final class LogConfig {
	private static Log log = LogFactory.getLog(LogConfig.class);
	private static Cache<String, Boolean> cache = CacheManagerImp.getCacheManager().getCache(LogConfig.class.getName(), false);
	
	public static boolean isHistoriable(Class<? extends Entity> cls) {
		if(TypeUtils.isImplementsFrom(cls, Logable.class)) return false;
		if(TypeUtils.isImplementsFrom(cls, whf.framework.meta.entity.Object.class)) return false;
		if(TypeUtils.isImplementsFrom(cls, whf.framework.meta.entity.ObjectProperty.class)) return false;
		if(cache.isEmpty()) {
			synchronized(cache) {
				try{
					DataSource ds = BeanFactory.getDataSource();
					Connection con = null;
					Statement stmt = null;
					ResultSet rs = null;
					try{
						con = ds.getConnection();
						stmt = con.createStatement();
						rs = stmt.executeQuery("select bo_class_name,auditable from framework_meta_object");
						while(rs.next()) {
							cache.put(rs.getString(1), rs.getInt(2) > 0 );
						}
					} finally {
						if(rs != null) rs.close();
						if(stmt != null) stmt.close();
						if(con != null) con.close();
					}
				}catch(Exception e){
					log.error(e);
				}
			}
		}
		Boolean b = cache.get(cls.getName());
		return b != null? b.booleanValue(): false;
	}
	
	public static boolean isAuditHistoryDetail() {
		return true;
	}
	
	public static String getDetailLogFormat() {
		return "属性'%1'从%2变到%3;<br>";
	}
}
